﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;

namespace HandRise.MBHOP.Controls
{
    public class TitleBar : Control
    {
        #region 属性

        /// <summary>
        /// 标题文本
        /// </summary>
        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                if (base.Text != value)
                {
                    base.Text = value;
                    if (titleRectangle.Height > 0)
                    {
                        this.Invalidate(titleRectangle);
                        this.Update();
                    }
                }
            }
        }

        /// <summary>
        /// 标题文本
        /// </summary>
        public override Color ForeColor
        {
            get
            {
                return base.ForeColor;
            }
            set
            {
                if (base.ForeColor != value)
                {
                    base.ForeColor = value;
                    if (titleRectangle.Height > 0)
                    {
                        this.Invalidate(titleRectangle);
                        this.Update();
                    }
                }
            }
        }

        private int fontSize = 12;
        /// <summary>
        /// 字号
        /// </summary>
        [System.ComponentModel.DefaultValue(12)]
        public int FontSize
        {
            get
            {
                return fontSize;
            }
            set
            {
                if (fontSize != value)
                {
                    if (value < 0)
                        value = 0;
                    fontSize = value;
                    if (titleRectangle.Height > 0)
                    {
                        this.Invalidate(titleRectangle);
                        this.Update();
                    }
                }
            }
        }



        private Image background = null;
        /// <summary>
        /// 背景图片
        /// </summary>
        public Image Background
        {
            get
            {
                return background;
            }
            set
            {
                if (background != value)
                {
                    background = value;
                    this.Refresh();
                }
            }
        }

        private ImageList iconList = null;
        /// <summary>
        /// 图标列表
        /// </summary>
        public ImageList IconList
        {
            get
            {
                return iconList;
            }
            set
            {
                if (value != null)
                {
                    if (iconList == null || value.Images.Count != iconList.Images.Count)
                    {
                        iconList = value;
                        this.Refresh();
                    }
                    else
                    {
                        iconList = value;
                        this.Invalidate(iconRectangle);
                        this.Update();
                    }
                }
                else
                {
                    iconList = null;
                    this.Refresh();
                }
            }
        }

        private int iconRightSpace = 0;
        /// <summary>
        /// 图标右边距

        /// </summary>
        public int IconRightSpace
        {
            get
            {
                return iconRightSpace;
            }
            set
            {
                if (value < 0)
                    value = 0;
                if (iconRightSpace != value)
                {
                    iconRightSpace = value;
                    this.Refresh();
                }
            }
        }

        private int titleLeftSpace = 0;
        /// <summary>
        /// 标题左边距

        /// </summary>
        public int TitleLeftSpace
        {
            get
            {
                return titleLeftSpace;
            }
            set
            {
                if (value < 0)
                    value = 0;
                if (titleLeftSpace != value)
                {
                    titleLeftSpace = value;
                    this.Refresh();
                }
            }
        }

        private int imageIndex0 = 0;
        /// <summary>
        /// 图标1index

        /// </summary>
        public int ImageIndex0
        {
            get
            {
                return imageIndex0;
            }
            set
            {
                if (value < 0)
                    value = 0;
                if (imageIndex0 != value)
                {
                    imageIndex0 = value;
                    this.Refresh();
                }
            }
        }
        private int imageIndex1 = 1;
        /// <summary>
        /// 图标2index

        /// </summary>
        public int ImageIndex1
        {
            get
            {
                return imageIndex1;
            }
            set
            {
                if (value < 0)
                    value = 0;
                if (imageIndex1 != value)
                {
                    imageIndex1 = value;
                    this.Refresh();
                }
            }
        }

        private int imageIndex2 = 2;
        /// <summary>
        /// 图标3index

        /// </summary>
        public int ImageIndex2
        {
            get
            {
                return imageIndex2;
            }
            set
            {
                if (value < 0)
                    value = 0;
                if (imageIndex2 != value)
                {
                    imageIndex2 = value;
                    this.Refresh();
                }
            }
        }

        /// <summary>
        /// 图标的间距

        /// </summary>
        int iconMargin = 0;

        /// <summary>
        /// 标题显示的区域

        /// </summary>
        Rectangle titleRectangle = new Rectangle(0, 0, Screen.PrimaryScreen.Bounds.Width, 0);

        /// <summary>
        /// 图标显示的区域

        /// </summary>
        Rectangle iconRectangle = new Rectangle(Screen.PrimaryScreen.Bounds.Width, 0, 0, 0);

        //放置处理过的图标及图标位置

        List<Point> iconLocationList = new List<Point>();

        //Bitmap bitBackground = null;

        public int exeTime = 0;

        private int exeTime1 = 0;
        private int exeTime2 = 0;

        #endregion

        /// <summary>
        /// 构造函数

        /// </summary>
        public TitleBar()
        {
            Width = Screen.PrimaryScreen.Bounds.Width;
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            int starttime = System.Environment.TickCount;
            base.OnPaintBackground(e);
            //bitBackground = new Bitmap(this.Width, this.Height);
            //Graphics.FromImage(bitBackground).DrawImage(background, this.ClientRectangle, new Rectangle(0, 0, background.Width, background.Height), GraphicsUnit.Pixel);

            if (background != null)
                e.Graphics.DrawImage(background, this.ClientRectangle, new Rectangle(0, 0, background.Width, background.Height), GraphicsUnit.Pixel);

            exeTime1 = System.Environment.TickCount - starttime;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            int starttime = System.Environment.TickCount;
            base.OnPaint(e);
            Width = Screen.PrimaryScreen.Bounds.Width;
            //两个边距长度不能超过宽度的一半

            //if (iconRightSpace > Width / 2)
            //    iconRightSpace = Width / 2;
            //if (titleLeftSpace > Width / 2)
            //    titleLeftSpace = Width / 2;

            GetLayout();

            //用于弹出框区域空白,当刷新区域跨图标区域和文字区域时才全部刷新，否则只刷新相应的区域
            bool drawTitle = false;
            bool drawIcon = false;
            if (e.ClipRectangle.X < iconRectangle.X)
            {
                drawTitle = true;
                if (e.ClipRectangle.Width > iconRectangle.X - e.ClipRectangle.X)
                    drawIcon = true;
            }
            else
                drawIcon = true;


            //画图标

            if (iconRightSpace <= Width / 2)
                if (e.ClipRectangle == iconRectangle || e.ClipRectangle == this.ClientRectangle || drawIcon)
                {
                    GetIconLocation();
                    DrawIcon(e.Graphics, iconRectangle);
                }
            //画文字

            if (titleLeftSpace <= Width / 2)
                if (e.ClipRectangle == titleRectangle || e.ClipRectangle == this.ClientRectangle || drawTitle)
                {
                    if (FontSize > 0)
                        DrawTitle(e.Graphics, (RectangleF)titleRectangle);
                }

            exeTime2 = System.Environment.TickCount - starttime;
            exeTime = exeTime1 + exeTime2;
        }

        /// <summary>
        /// 获得图标及文字的区域
        /// </summary>
        void GetLayout()
        {
            //图标
            if (iconList != null && iconList.Images.Count > 0)
            {
                int iconCount = iconList.Images.Count > 3 ? 3 : iconList.Images.Count;
                //图标的长度+间隙的长度，图标框的宽度为控件的高

                int iconListWidth = this.Height * iconCount + iconMargin * (iconCount - 1);
                //如果图标显示的区域大于控件宽度的一半的话，图标区域X坐标为空间中间点,图标区域宽度为控件一半，否则按照实际大小
                //if (iconListWidth + iconRightSpace > Width / 2)
                //{
                //    iconRectangle.X = Width / 2;
                //    if (iconRightSpace < Width / 2)
                //        iconRectangle.Width = Width / 2 - iconRightSpace;
                //    else
                //        iconRectangle.Width = 0;
                //}
                //else
                //{
                if (Width > iconListWidth + iconRightSpace)
                    iconRectangle.X = Width - iconListWidth - iconRightSpace;
                iconRectangle.Width = Width - iconRectangle.X - iconRightSpace;
                //}
                iconRectangle.Height = this.Height;
            }
            //文字
            if (this.Text != "")
            {
                titleRectangle.X = titleLeftSpace;
                if (iconRectangle.X > titleLeftSpace)
                    titleRectangle.Width = iconRectangle.X - titleLeftSpace;
                else
                    titleRectangle.Width = 0;

                if (iconRightSpace > Width / 2)
                    titleRectangle.Width = this.Width;
                titleRectangle.Height = this.Height;
            }
        }

        /// <summary>
        /// 画标题

        /// </summary>
        /// <param name="g"></param>
        void DrawTitle(Graphics g, RectangleF titleRectangle)
        {
            Font font = new Font("雅黑", FontSize, FontStyle.Regular);
            SizeF size = g.MeasureString(this.Text, font);
            if (size.Height < this.Height)
                titleRectangle.Y = (this.Height - Convert.ToInt32(size.Height)) / 2;
            titleRectangle.Height = size.Height;
            g.DrawString(GetShowString(g), font, new SolidBrush(this.ForeColor), titleRectangle);
        }


        /// <summary>
        /// 画图标

        /// </summary>
        /// <param name="g"></param>
        void DrawIcon(Graphics g, Rectangle iconRectangle)
        {

            int[] imageindex = new int[3];
            imageindex[0] = imageIndex0;
            imageindex[1] = imageIndex1;
            imageindex[2] = imageIndex2;
  
            if (iconList != null)
            {
                ImageAttributes imageAttr = new ImageAttributes();
                for (int i = 0; i < iconLocationList.Count; i++)
                {
                    Color startColor = ((Bitmap)(IconList.Images[imageindex[i]])).GetPixel(0, 0);
                    imageAttr.SetColorKey(startColor, startColor);
                    //if (iconLocationList[i].X >= this.Width / 2)
                    g.DrawImage(IconList.Images[imageindex[i]],
                        new Rectangle(iconLocationList[i].X, iconLocationList[i].Y, IconList.Images[imageindex[i]].Width, IconList.Images[imageindex[i]].Height),
                        0, 0, IconList.Images[imageindex[i]].Width, IconList.Images[imageindex[i]].Height, GraphicsUnit.Pixel, imageAttr);
                    //    else if (this.Width / 2 - iconLocationList[i].X < IconList.Images[i].Width)
                    //    g.DrawImage(IconList.Images[i],
                    //    new Rectangle(this.Width / 2, iconLocationList[i].Y, IconList.Images[i].Width - (this.Width / 2 - iconLocationList[i].X), IconList.Images[i].Height),
                    //    this.Width / 2 - iconLocationList[i].X, 0, IconList.Images[i].Width - (this.Width / 2 - iconLocationList[i].X), IconList.Images[i].Height, GraphicsUnit.Pixel, imageAttr);
                }
            }
        }
        //获得图标的坐标

        void GetIconLocation()
        {
            if (iconList != null && iconList.Images.Count > 0)
            {
                iconLocationList.Clear();
                for (int i = 0; i < (iconList.Images.Count < 3 ? iconList.Images.Count : 3); i++)
                {
                    int x, y = 0;
                    //竖直居中
                    if (iconList.Images[i].Height < this.Height)
                    {
                        y = (this.Height - iconList.Images[i].Height) / 2;
                        //}
                        ////从图表区域右边开始计算

                        //if (iconList.Images[i].Height < this.Width)
                        //{
                        //图标区域X坐标+图标区域宽度-图标框长度-路标空隙长度+取居中的值

                        x = iconRectangle.X + iconRectangle.Width - (i + 1) * this.Height - i * iconMargin + (this.Height - iconList.Images[i].Height) / 2;
                    }
                    else
                    {
                        //图标区域X坐标+图标区域宽度-图标框长度-路标空隙长度
                        x = iconRectangle.X + iconRectangle.Width - (i + 1) * this.Height - i * iconMargin;
                    }

                    iconLocationList.Add(new Point(x, y));
                }
            }

        }

        private string GetShowString(Graphics g)
        {
            Font font = new Font("雅黑", FontSize, FontStyle.Regular);
            StringBuilder sb = new StringBuilder(this.Text);
            SizeF size = g.MeasureString(sb.ToString(), font);
            if (size.Width > titleRectangle.Width)
            {
                for (int i = 0; i < sb.Length; i++)
                {

                    size = g.MeasureString(sb.ToString(0, i), font);

                    if (size.Width > titleRectangle.Width)
                    {
                        //if (i > 1)
                        return sb.ToString(0, i);
                        //else
                        //    return sb.ToString(0, 1);
                    }
                }
            }
            return sb.ToString();
        }

    }
}
